package com.kalaqia.ali.pay;

import android.app.Activity;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;

/**
 * function:
 * project:PayModule
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2017/8/31 15:28
 */
//<editor-fold desc="version update log">
// updare owner:kalaqia  421407393@qq.com
// update datetime:2017/8/31 15:28 
// update remark:
//</editor-fold>
public class AliPayClient {

    private Activity activity;
    private PayCallback callback;
    private String orderInfo;

    public AliPayClient(Activity activity, String orderInfo) {
        this.activity = activity;
        this.orderInfo = orderInfo;
    }

    public void send() {
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask payTask = new PayTask(activity);
                final PayResult payResult = new PayResult(payTask.payV2(orderInfo, true));
                if (activity != null)
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (TextUtils.equals(payResult.getResultStatus(), "9000")) {
                                // 订单支付成功
                                callback.onSucceed(payResult);
                            } else if (TextUtils.equals(payResult.getResultStatus(), "8000")) {
                                callback.onError(payResult);
                            } else if (TextUtils.equals(payResult.getResultStatus(), "4000")) {
                                // 订单支付失败
                                callback.onError(payResult);
                            } else if (TextUtils.equals(payResult.getResultStatus(), "6001")) {
                                callback.onError(payResult);
                            }
                        }
                    });
            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

}
